package com.weipeep.sys.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.google.common.collect.Lists;
import com.weipeep.common.utils.IdGenerator;
import com.weipeep.common.utils.MD5Utils;
import com.weipeep.sys.dao.UserDao;
import com.weipeep.sys.dao.UserRoleDao;
import com.weipeep.sys.domain.UserDO;
import com.weipeep.sys.domain.UserRoleDO;
import com.weipeep.sys.query.StaffQuery;
import com.weipeep.sys.vo.StaffVo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.weipeep.sys.dao.StaffDao;
import com.weipeep.sys.domain.StaffDO;
import com.weipeep.sys.service.StaffService;
import com.weipeep.common.base.CoreServiceImpl;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * 
 * <pre>
 * 
 * </pre>
 * <small> 2018-04-19 11:24:57 | wangyun</small>
 */
@Service
public class StaffServiceImpl extends CoreServiceImpl<StaffDao, StaffDO> implements StaffService {
    @Resource
    private StaffDao staffDao;
    @Autowired
    private IdGenerator idGenerator;
    @Resource
    UserDao userDao;
    @Resource
    UserRoleDao userRoleDao;
    @Override
    @Transactional
    public boolean insertStaff(StaffVo staffVo) {
        Long userId = idGenerator.nextId();
        Long studentId = idGenerator.nextId();
        UserDO userDO = new UserDO();
        userDO.setBirth(staffVo.getBirth());
        userDO.setSex(staffVo.getSex());
        userDO.setName(staffVo.getRealName());
        userDO.setMobile(staffVo.getMobile());
        userDO.setEmail(staffVo.getEmail());
        userDO.setLiveAddress(staffVo.getAddress());
        userDO.setStatus(1);
        userDO.setId(userId);
        userDO.setUsername(staffVo.getUsername());
        if (StringUtils.isNotEmpty(staffVo.getUsername()) && StringUtils.isNotEmpty(staffVo.getPassword())){
            userDO.setPassword(MD5Utils.encrypt(staffVo.getUsername(), staffVo.getPassword()));
        }
        staffVo.setUserId(userId);
        staffVo.setId(studentId);
        Integer userNum = userDao.insert(userDO);
        List<Long> roles = staffVo.getRoleIds();
        userRoleDao.removeByUserId(userId);
        List<UserRoleDO> list = new ArrayList<>();
        for (Long roleId : roles) {
            UserRoleDO ur = new UserRoleDO();
            ur.setUserId(userId);
            ur.setRoleId(roleId);
            list.add(ur);
        }
        if (list.size() > 0) {
            userRoleDao.batchSave(list);
        }
        Integer studentNum = staffDao.insert(staffVo);
        return userNum==1 && studentNum==1;
    }

    @Override
    @Transactional
    public boolean updateStaff(StaffVo staffVo) {
        StaffDO staffDO = staffDao.selectById(staffVo.getId());
        staffDO.setEntryTime(staffVo.getEntryTime());
        staffDO.setIdCard(staffVo.getIdCard());
        staffDO.setLeaveTime(staffVo.getLeaveTime());
        staffDO.setPosition(staffVo.getPosition());
        staffDO.setStaffNo(staffVo.getStaffNo());
        staffDO.setSalaryCard(staffVo.getSalaryCard());
        staffDO.setWorkStatus(staffVo.getWorkStatus());
        Long userId = staffDO.getUserId();
        UserDO userDO = userDao.selectById(userId);
        userDO.setBirth(staffVo.getBirth());
        userDO.setName(staffVo.getRealName());
        userDO.setSex(staffVo.getSex());
        userDO.setMobile(staffVo.getMobile());
        userDO.setEmail(staffVo.getEmail());
        userDO.setLiveAddress(staffVo.getAddress());
        userDO.setUsername(staffVo.getUsername());
        if (StringUtils.isNotEmpty(staffVo.getUsername()) && StringUtils.isNotEmpty(staffVo.getPassword())){
            userDO.setPassword(MD5Utils.encrypt(staffVo.getUsername(), staffVo.getPassword()));
        }
        List<Long> roles = staffVo.getRoleIds();
        userRoleDao.removeByUserId(userId);
        List<UserRoleDO> list = new ArrayList<>();
        for (Long roleId : roles) {
            UserRoleDO ur = new UserRoleDO();
            ur.setUserId(userId);
            ur.setRoleId(roleId);
            list.add(ur);
        }
        if (list.size() > 0) {
            userRoleDao.batchSave(list);
        }
        return userDao.updateById(userDO)==1 && staffDao.updateById(staffDO)==1;
    }

    @Override
    @Transactional
    public boolean deleteStaffById(Long id) {
        StaffDO staffDO = staffDao.selectById(id);
        return userDao.deleteById(staffDO.getUserId())==1&&staffDao.deleteById(id)==1;
    }

    @Override
    @Transactional
    public void deleteStaffBatchIds(List<Long> ids) {
        if (ids == null || ids.size()==0){
            return;
        }
        List<Long> userIds = Lists.newArrayList();
        List<StaffDO> staffDOList = staffDao.selectBatchIds(ids);
        staffDOList.forEach(staffDO -> {
            if (staffDO.getUserId()!=null){
                userIds.add(staffDO.getUserId());
            }
        });
        userDao.deleteBatchIds(userIds);
        staffDao.deleteBatchIds(ids);
    }

    @Override
    public Page<StaffVo> selectStaffPage(Page<StaffVo> page, StaffVo staff) {
        StaffQuery staffQuery = new StaffQuery();
        staffQuery.setOffset(page.getSize()*(page.getCurrent()-1));
        staffQuery.setLimit(page.getSize());
        if (StringUtils.isNotEmpty(staffQuery.getRealName())){
            staffQuery.setRealName(staffQuery.getRealName()+"%");
        }
        List<StaffVo> staffVoList = staffDao.selectStaffPage(staffQuery);
        page.setRecords(staffVoList);
        return page;
    }

    @Override
    public int selectStaffCount(StaffVo staff) {
        StaffQuery staffQuery = new StaffQuery();
        if (StringUtils.isNotEmpty(staff.getRealName())){
            staff.setRealName(staff.getRealName()+"%");
        }
        int count = staffDao.selectStaffCount(staffQuery);
        return count;
    }

    @Override
    public List<StaffVo> selectStaffList(StaffVo staffVo) {
        StaffQuery staffQuery = new StaffQuery();
        if (StringUtils.isNotEmpty(staffVo.getRealName())){
            staffQuery.setRealName(staffVo.getRealName()+"%");
        }
        staffQuery.setDeptId(staffVo.getDeptId());
        List<StaffVo> staffVoList = staffDao.selectStaffList(staffQuery);
        return staffVoList;
    }
}
